package com.hushao.aimi.service.impl;

import com.hushao.aimi.bo.CartBo;
import com.hushao.aimi.vo.CartVo;
import com.hushao.aimi.common.ServerResponse;
import com.hushao.aimi.entity.AimiCart;
import com.hushao.aimi.entity.AimiProductSpec;
import com.hushao.aimi.repository.CartRepository;
import com.hushao.aimi.repository.ProductRespository;
import com.hushao.aimi.repository.ProductSpecRepository;
import com.hushao.aimi.repository.UserRepository;
import com.hushao.aimi.service.CartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @description: 购物车服务
 * @author: 胡少
 * @create: 2018-06-08 09:54
 **/

@Slf4j
@Service
public class CartServiceImpl implements CartService {
    @Autowired
    private CartRepository cartRepository;
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private ProductRespository productRespository;
    @Autowired
    private ProductSpecRepository productSpecRepository;

    @Override
    public ServerResponse getCartByUsername(String username) {
        List<Object> objects=cartRepository.getProductSpecsByUsername(username);
        //获取当前用户购物车中所有的商品规格并根据商品进行分组
        Map<String,List<CartBo>> productVos=objects.stream().map(CartBo::getCartBo).collect(Collectors.groupingBy(CartBo::getProductId));

        List<CartVo> cartVos=productVos.entrySet().stream()
                .map(m-> new CartVo(m.getKey(),m.getValue().get(0).getProductName(),CartBo.getCartBo(m.getValue()))).collect(Collectors.toList());
        return ServerResponse.Success(cartVos);
    }



    @Override
    public ServerResponse add(String username, String productSpecId, Integer count) throws Exception {
        String userId=userRepository.findByUsername(username).getId();

            //判断购买的数量是否小于其库存数量
            if(isLimitNumber(count,productSpecId)){
                AimiCart cartItem=new AimiCart(userId,productSpecId,count);
                cartRepository.save(cartItem);
                return ServerResponse.Success();
            }else {
            return ServerResponse.Error("你选择此规格的产品数量超过其库存数量");}

    }

    @Override
    public ServerResponse update(String cartId, Integer count) throws Exception {
        Optional<AimiCart> optional=cartRepository.findById(cartId);
        if (optional.isPresent()){
            AimiCart cart=optional.get();
            if(isLimitNumber(count,cart.getProductSpecId())){
                cart.setNumber(count);
                cartRepository.save(cart);
                return ServerResponse.Success();
            }else{
                return ServerResponse.Error("你选择此规格的产品数量超过其库存数量");}
        }
        return ServerResponse.Error("未知错误");

    }

    public boolean isLimitNumber(int count,String productSpecId){
       Optional<AimiProductSpec> optional= productSpecRepository.findById(productSpecId);
       if(optional.isPresent()){
           if(optional.get().getStock()>=count){
               return true;
           }
       }
       return false;
    }
}
